﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TRKUTIL.Text
{
    [TestClass]
    public class TrkutilTester
    {
        const string ExeFile = "TRKUTIL.EXE";
        const string FileGenerated = "FileGenerated.TXT";
        const string FileConfig = "TRKUTIL.conf";
#if DEBUG
        const string FilePath = "DEBUG";
#else
        const string FilePath = "RELEASE";
#endif

        private bool fileCopied = false;

        [TestInitialize]
        public void Initialize()
        {
            if (!File.Exists(ExeFile))
            {
                var srcFile = Path.Combine("../../../" + FilePath, ExeFile);
                if (File.Exists(srcFile))
                {
                    File.Copy(srcFile, ExeFile);
                    fileCopied = true;
                }
                
                Assert.IsTrue(File.Exists(ExeFile), ExeFile + " is not found");
            }
        }

        [TestCleanup]
        public void Cleanup()
        {
            if (File.Exists(FileGenerated))
                File.Delete(FileGenerated);
            if (File.Exists(FileConfig))
                File.Delete(FileConfig);
            if (fileCopied)
                File.Delete(ExeFile);
        }

        [TestMethod]
        public void TestOK()
        {
            File.WriteAllText(FileConfig, "Command=ECHO>" + FileGenerated + "\nLogLevel=Warning");
            var process = ProcessRunner.Run(ExeFile, "SendEvent HeartBeat=1,A=2,B=3");
            Assert.AreEqual(0, process.ExitCode);
            Assert.AreEqual(string.Empty, process.Error);
            Assert.IsTrue(File.Exists(FileGenerated));
            Assert.AreEqual("\"HeartBeat=1;A=2;B=3\"" + Environment.NewLine, File.ReadAllText(FileGenerated));
        }

        [TestMethod]
        public void TestInvalidArguments()
        {
            File.WriteAllText(FileConfig, "Command=ECHO>" + FileGenerated + "\nLogLevel=Warning");
            var process = ProcessRunner.Run(ExeFile, "SendEvent");
            Assert.AreEqual(1, process.ExitCode);
            Assert.IsFalse(File.Exists(FileGenerated));
            Assert.AreEqual(string.Empty, process.Output);
            Assert.IsTrue(process.Error.StartsWith("Error number of parameters"));
        }

        [TestMethod]
        public void TestProgramNotFound()
        {
            File.WriteAllText(FileConfig, "Command=SomethingDoesntExisting\nLogLevel=Warning");
            var process = ProcessRunner.Run(ExeFile, "SendEvent a=1,b=2");
            Assert.AreEqual(1, process.ExitCode);
            Assert.IsFalse(File.Exists(FileGenerated));
            Assert.AreEqual(string.Empty, process.Output);
            Assert.IsTrue(process.Error.Contains("Error executing"));
        }

        [TestMethod]
        public void TestUnknownParameter()
        {
            File.WriteAllText(FileConfig, "Command=ECHO>" + FileGenerated + "\nLogLevel=Warning");
            var process = ProcessRunner.Run(ExeFile, "CreateEvent HeartBeat=5");
            Assert.AreEqual(0, process.ExitCode);
            Assert.IsTrue(process.Error.StartsWith("Unknown TRKUTIL parameter type found: CreateEvent"));
            Assert.IsTrue(File.Exists(FileGenerated));
            Assert.AreEqual("\"HeartBeat=5\"" + Environment.NewLine, File.ReadAllText(FileGenerated));
        }
    }
}
